#include "stdint.h"
#include "string.h"

#include "define.h"
#include "app_ota.h"
#include "app_flash.h"

extern OTAParaFame ota;
extern DataFrame Data;

//extern FlagStruct Flag;
//extern TimeStruct TimeFlag;
/****************************************************************
*Name:    getHardwareVersion
*Function:  解析流程
*Input:   version 硬件版本号，用来区别升级文件是否兼容实际硬件
*Output:  0：硬件不兼容，1：硬件兼容
*Author:    gm_jeng
*Date:    2021/7/29
*E-Mail:  gm_jeng@163.com
*Other:   static函数仅限ota.c使用
****************************************************************/
static uint8_t getHardwareVersion(void){
#if defined (STM32F10X_LD) || defined (STM32F10X_LD_VL) || defined (STM32F10X_MD) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || defined (STM32F10X_XL) && !defined (STM32F10X_CL)
  return STM32F103RCT6_VERSION;
#elif defined (STM32F4XX) || defined (STM32F40XX) || defined (STM32F427X)
  return STM32F401RCT6_VERSION;
#elif defined EFR32FG14P231F128GM32
  return EFR32FG14P231F128GM32_VERSION;
#else
  return 0xFF;
#endif
}


struct private_ota
{
    uint8_t sameFlag;
    uint8_t lock;
    int8_t cnt;
};

struct private_ota pri_ota = {0};

uint8_t test[32] = {0};
volatile uint8_t val = 0;

void BeaconToOta(uint8_t * rxbuf)
{
    uint8_t filter_part_no_0[] = {Data.LoPartNo[0], 0x00, 0x00, 0x00};
    uint8_t filter_part_no_F[] = {Data.LoPartNo[0], 0xff, 0xff, 0xff};
    uint8_t SameVersionFlag = 0;
    memcpy(test, rxbuf, 32);

    /*没有数据*/
    if(memcmp(&rxbuf[6], filter_part_no_0, 4) == 0)
    {
       /*do nothing!!*/
    }
    /*组播*/
    else if(memcmp(&rxbuf[6], filter_part_no_F, 4) == 0)
    {
       if( 0xF1 == rxbuf[11])  /*是否为请求升级报文*/
       {
           if(getHardwareVersion() != rxbuf[15]) //比较芯片型号
           {
               /*do nothing!!*/
           }
           else if(memcmp(&rxbuf[12], &ota.para.ota_version0[0], 3) == 0)//版本号相同
           {
               SameVersionFlag = 1;//相同版本,直接返回升级成功
           }
           else
           {
               ota.para.ota_state = 0;
               memcpy(&ota.para.ota_version1[0], &rxbuf[12], 3);
//               OTAFlashOperation();
               NVIC_SystemReset();
           }
       }
    }
    /*单播*/
    else if(memcmp(&Data.LoPartNo[0], &rxbuf[6], 4) == 0)
    {
       if( 0xF1 == rxbuf[11])  /*是否为请求升级报文*/
       {
           if(getHardwareVersion() != rxbuf[15]) //比较芯片型号
           {
               /*do nothing!!*/
           }
           else if(memcmp(&rxbuf[12], &ota.para.ota_version0[0], 3) == 0)//版本号相同
           {
               SameVersionFlag = 1; //相同版本,直接返回升级成功
           }
           else
           {
               ota.para.ota_state = 0;
               memcpy(&ota.para.ota_version1[0], &rxbuf[12], 3);
//               OTAFlashOperation();
               __NVIC_SystemReset();
           }
       }
    }
}


void SameVersionCounter(void)
{
    if(1 == pri_ota.lock)
    {
        pri_ota.cnt --;
        if(pri_ota.cnt <= 0)
        {
            pri_ota.lock = 0; //加锁

            pri_ota.sameFlag = 0;
        }
    }
}


void ActionToOta(uint8_t * rxbuf)
{
    if(rxbuf[15] == 0x06) //基站请求升级报文
    {
        if(getHardwareVersion() != rxbuf[19]) //比较芯片型号
        {
            /*do nothing!!*/
        }
        else if(memcmp(&rxbuf[16], &ota.para.ota_version0[0], 3) == 0)//版本号相同
        {
           /*do nothing!!*/
        }
        else
        {
            ota.para.ota_state = 0;
            memcpy(&ota.para.ota_version1[0], &rxbuf[16], 3);
            __disable_irq();
            OTAFlashOperation();
            __NVIC_SystemReset();
        }
    }
}
